package com.example.service;

import com.example.entity.Account;
import com.example.entity.Lunbo;
import com.example.mapper.LunboMapper;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LunboService {

    @Resource
    LunboMapper lunboMapper;

    public void add(Lunbo lunbo) {
        Account accountUser = TokenUtils.getCurrentUser();
        if ( "ADMIN".equals(accountUser.getRole())){
            lunbo.setAdminId(accountUser.getId());
        }
        lunboMapper.insert(lunbo);
    }

    public void delete(Integer id) {
        lunboMapper.deleteById(id);
    }

    public void update(Lunbo lunbo) {
        lunboMapper.updateById(lunbo);
    }

    public List<Lunbo> selectAll(Lunbo lunbo) {
        return lunboMapper.selectAll(lunbo);
    }

    public PageInfo<Lunbo> selectPage(Integer pageNum, Integer pageSize, Lunbo lunbo) {
        Account accountUser = TokenUtils.getCurrentUser();
        if ( "ADMIN".equals(accountUser.getRole())){
            lunbo.setAdminId(accountUser.getId());
        }
        // 开启分页查询
        PageHelper.startPage(pageNum, pageSize);
        List<Lunbo> list = lunboMapper.selectAll(lunbo);
        return PageInfo.of(list);
    }



}
